function f = betilfix(betil,betag,p,h,sigmac,zstar,sprd,ups,del,alp,Bigphi,Lstar,gam,gstar,pistar,bgystar);


rstar = (1/betil)*exp(sigmac*zstar);

rkstar = sprd*rstar*ups - (1-del);%NOTE: includes "sprd*"

wstar = (alp^(alp)*(1-alp)^(1-alp)*rkstar^(-alp)/Bigphi)^(1/(1-alp));



kstar = (alp/(1-alp))*wstar*Lstar/rkstar;

kbarstar = kstar*(gam+1)*ups^(1/(1-alp));

istar = kbarstar*( 1-((1-del)/((gam+1)*ups^(1/(1-alp)))) );

ystar = (kstar^alp)*(Lstar^(1-alp))/Bigphi;

cstar = (1-gstar)*ystar - istar;


%% new stuff
nu_log = wstar*(1-Lstar)/( cstar*(1-h*exp(-zstar)) ); %%##

bgstar = bgystar*ystar;

tstar = ystar*gstar + (1-(1/rstar)*exp(zstar))*(exp(zstar)/pistar)*bgstar;  %%##

s_c = (cstar-wstar*Lstar+tstar)/((1-betil)*cstar); %%##


etabet = (1 + p * (1-betag*(1-p)) * s_c/( (1-p)*(1+nu_log)*(1-h*exp(-zstar)) ) )^(-1);

betil_ = etabet*betag;

f = 100*abs(betil_-betil);
end
